package zz.note.zzjustauth.controller;

import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthAlipayRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: ZZ
 * @date 2022/2/8
 */
@RestController
@RequestMapping("/alipay")
public class AliPayAuth {
	@RequestMapping("/render")
	public void renderAuth(HttpServletResponse response) throws IOException {
		AuthRequest authRequest = getAuthRequest();
		response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
	}

	@RequestMapping("/callback")
	public Object login(AuthCallback callback) {
		AuthRequest authRequest = getAuthRequest();
		return authRequest.login(callback);
	}

	private AuthRequest getAuthRequest() {
		return new AuthAlipayRequest(AuthConfig.builder()
				.clientId("APPID")
				.clientSecret("应用私钥")
				.alipayPublicKey("支付宝公钥")
				.redirectUri("https://www.zhyd.me/oauth/callback/alipay")
				.build());
	}
}
